{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 9.6 \u5e26\u53ef\u9009\u53c2\u6570\u7684\u88c5\u9970\u5668\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u5199\u4e00\u4e2a\u88c5\u9970\u5668\uff0c\u65e2\u53ef\u4ee5\u4e0d\u4f20\u53c2\u6570\u7ed9\u5b83\uff0c\u6bd4\u5982 @decorator \uff0c\n\u4e5f\u53ef\u4ee5\u4f20\u9012\u53ef\u9009\u53c2\u6570\u7ed9\u5b83\uff0c\u6bd4\u5982 @decorator(x,y,z) \u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u662f9.5\u5c0f\u8282\u4e2d\u65e5\u5fd7\u88c5\u9970\u5668\u7684\u4e00\u4e2a\u4fee\u6539\u7248\u672c\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from functools import wraps, partial\nimport logging\n\ndef logged(func=None, *, level=logging.DEBUG, name=None, message=None):\n    if func is None:\n        return partial(logged, level=level, name=name, message=message)\n\n    logname = name if name else func.__module__\n    log = logging.getLogger(logname)\n    logmsg = message if message else func.__name__\n\n    @wraps(func)\n    def wrapper(*args, **kwargs):\n        log.log(level, logmsg)\n        return func(*args, **kwargs)\n\n    return wrapper\n\n# Example use\n@logged\ndef add(x, y):\n    return x + y\n\n@logged(level=logging.CRITICAL, name='example')\ndef spam():\n    print('Spam!')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53ef\u4ee5\u770b\u5230\uff0c@logged \u88c5\u9970\u5668\u53ef\u4ee5\u540c\u65f6\u4e0d\u5e26\u53c2\u6570\u6216\u5e26\u53c2\u6570\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u91cc\u63d0\u5230\u7684\u8fd9\u4e2a\u95ee\u9898\u5c31\u662f\u901a\u5e38\u6240\u8bf4\u7684\u7f16\u7a0b\u4e00\u81f4\u6027\u95ee\u9898\u3002\n\u5f53\u6211\u4eec\u4f7f\u7528\u88c5\u9970\u5668\u7684\u65f6\u5019\uff0c\u5927\u90e8\u5206\u7a0b\u5e8f\u5458\u4e60\u60ef\u4e86\u8981\u4e48\u4e0d\u7ed9\u5b83\u4eec\u4f20\u9012\u4efb\u4f55\u53c2\u6570\uff0c\u8981\u4e48\u7ed9\u5b83\u4eec\u4f20\u9012\u786e\u5207\u53c2\u6570\u3002\n\u5176\u5b9e\u4ece\u6280\u672f\u4e0a\u6765\u8bb2\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u6240\u6709\u53c2\u6570\u90fd\u662f\u53ef\u9009\u7684\u88c5\u9970\u5668\uff0c\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "@logged()\ndef add(x, y):\n    return x+y"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f46\u662f\uff0c\u8fd9\u79cd\u5199\u6cd5\u5e76\u4e0d\u7b26\u5408\u6211\u4eec\u7684\u4e60\u60ef\uff0c\u6709\u65f6\u5019\u7a0b\u5e8f\u5458\u5fd8\u8bb0\u52a0\u4e0a\u540e\u9762\u7684\u62ec\u53f7\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002\n\u8fd9\u91cc\u6211\u4eec\u5411\u4f60\u5c55\u793a\u4e86\u5982\u4f55\u4ee5\u4e00\u81f4\u7684\u7f16\u7a0b\u98ce\u683c\u6765\u540c\u65f6\u6ee1\u8db3\u6ca1\u6709\u62ec\u53f7\u548c\u6709\u62ec\u53f7\u4e24\u79cd\u60c5\u51b5\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u7406\u89e3\u4ee3\u7801\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u4f60\u9700\u8981\u975e\u5e38\u719f\u6089\u88c5\u9970\u5668\u662f\u5982\u4f55\u4f5c\u7528\u5230\u51fd\u6570\u4e0a\u4ee5\u53ca\u5b83\u4eec\u7684\u8c03\u7528\u89c4\u5219\u3002\n\u5bf9\u4e8e\u4e00\u4e2a\u50cf\u4e0b\u9762\u8fd9\u6837\u7684\u7b80\u5355\u88c5\u9970\u5668\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Example use\n@logged\ndef add(x, y):\n    return x + y"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e2a\u8c03\u7528\u5e8f\u5217\u8ddf\u4e0b\u9762\u7b49\u4ef7\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def add(x, y):\n    return x + y\n\nadd = logged(add)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u65f6\u5019\uff0c\u88ab\u88c5\u9970\u51fd\u6570\u4f1a\u88ab\u5f53\u505a\u7b2c\u4e00\u4e2a\u53c2\u6570\u76f4\u63a5\u4f20\u9012\u7ed9 logged \u88c5\u9970\u5668\u3002\n\u56e0\u6b64\uff0clogged() \u4e2d\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u5c31\u662f\u88ab\u5305\u88c5\u51fd\u6570\u672c\u8eab\u3002\u6240\u6709\u5176\u4ed6\u53c2\u6570\u90fd\u5fc5\u987b\u6709\u9ed8\u8ba4\u503c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u800c\u5bf9\u4e8e\u4e00\u4e2a\u4e0b\u9762\u8fd9\u6837\u6709\u53c2\u6570\u7684\u88c5\u9970\u5668\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "@logged(level=logging.CRITICAL, name='example')\ndef spam():\n    print('Spam!')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8c03\u7528\u5e8f\u5217\u8ddf\u4e0b\u9762\u7b49\u4ef7\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def spam():\n    print('Spam!')\nspam = logged(level=logging.CRITICAL, name='example')(spam)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u521d\u59cb\u8c03\u7528 logged() \u51fd\u6570\u65f6\uff0c\u88ab\u5305\u88c5\u51fd\u6570\u5e76\u6ca1\u6709\u4f20\u9012\u8fdb\u6765\u3002\n\u56e0\u6b64\u5728\u88c5\u9970\u5668\u5185\uff0c\u5b83\u5fc5\u987b\u662f\u53ef\u9009\u7684\u3002\u8fd9\u4e2a\u53cd\u8fc7\u6765\u4f1a\u8feb\u4f7f\u5176\u4ed6\u53c2\u6570\u5fc5\u987b\u4f7f\u7528\u5173\u952e\u5b57\u6765\u6307\u5b9a\u3002\n\u5e76\u4e14\uff0c\u4f46\u8fd9\u4e9b\u53c2\u6570\u88ab\u4f20\u9012\u8fdb\u6765\u540e\uff0c\u88c5\u9970\u5668\u8981\u8fd4\u56de\u4e00\u4e2a\u63a5\u53d7\u4e00\u4e2a\u51fd\u6570\u53c2\u6570\u5e76\u5305\u88c5\u5b83\u7684\u51fd\u6570(\u53c2\u80039.5\u5c0f\u8282)\u3002\n\u4e3a\u4e86\u8fd9\u6837\u505a\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u4e2a\u6280\u5de7\uff0c\u5c31\u662f\u5229\u7528 functools.partial \u3002\n\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u672a\u5b8c\u5168\u521d\u59cb\u5316\u7684\u81ea\u8eab\uff0c\u9664\u4e86\u88ab\u5305\u88c5\u51fd\u6570\u5916\u5176\u4ed6\u53c2\u6570\u90fd\u5df2\u7ecf\u786e\u5b9a\u4e0b\u6765\u4e86\u3002\n\u53ef\u4ee5\u53c2\u80037.8\u5c0f\u8282\u83b7\u53d6\u66f4\u591a partial() \u65b9\u6cd5\u7684\u77e5\u8bc6\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}